package com.xiongxiong.yiqing.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiongxiong.yiqing.api.CommonController;
import com.xiongxiong.yiqing.api.CommonResult;
import com.xiongxiong.yiqing.exception.Asserts;
import com.xiongxiong.yiqing.pojo.SysUser;
import com.xiongxiong.yiqing.pojo.vo.FindSysUserCondition;
import com.xiongxiong.yiqing.pojo.vo.LoginVO;
import com.xiongxiong.yiqing.pojo.vo.SysUserVO;
import com.xiongxiong.yiqing.service.SysUserService;
import com.xiongxiong.yiqing.service.impl.SysUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 熊熊很酷
 * @since 2022-12-14
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户")
@Slf4j
public class SysUserController extends CommonController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("用户登入")
    @PostMapping("/login")
    public CommonResult<LoginVO> login(@RequestBody SysUserVO sysUserVO) {
        log.info("用户登入={}", sysUserVO);
        return process(() ->  sysUserService.login(sysUserVO));
    }

    @ApiOperation("用户注册或管理员添加")
    @PostMapping("/register")
    public CommonResult<SysUserVO> register(@RequestBody SysUserVO sysUserVO) {
        log.info("用户注册={}", sysUserVO);
        return process(() ->  sysUserService.register(sysUserVO));
    }

    @ApiOperation("分页或帅选条件查询")
    @PostMapping("/userList/{pageNub}/{pageSize}")
    public CommonResult<Page<SysUser>> selectUserListByPage(@PathVariable("pageNub") Integer pageNub,
                                                              @PathVariable("pageSize") Integer pageSize,
                                                              @RequestBody(required = false) FindSysUserCondition findCondition) {

        return process(() -> sysUserService.selectUserListByPage(pageNub, pageSize, findCondition));
    }

    @ApiOperation("更新")
    @PostMapping("/updateUserById")
    public CommonResult<Boolean> updateUserById(@RequestBody SysUser user) {
        log.info("用户更新={}", user);
        return process(() ->  sysUserService.updateUserById(user));
    }

    @ApiOperation("删除通过id")
    @GetMapping("/deleteUserById/{id}")
    public CommonResult<Boolean> deleteUserById(@PathVariable("id") Integer id) {
        return process(() ->  sysUserService.deleteUserById(id));
    }

    @ApiOperation("查询通过id")
    @GetMapping("/selectUserById/{id}")
    public CommonResult<SysUser> selectUserById(@PathVariable("id") Integer id) {
        return process(() ->  sysUserService.selectUserById(id));
    }

    @ApiOperation("修改密码")
    @GetMapping("/updatePassword/{id}")
    public CommonResult<Boolean> updatePassword(@PathVariable("id") Integer id, @RequestParam("oldPassword") String oldPassword,
                                                @RequestParam("newPassword") String newPassword) {
        log.info("修改密码={}=n新密码{}", oldPassword, newPassword);
        return process(() ->  sysUserService.updatePassword(id,newPassword));
    }

}
